package com.sunny.controller;

import com.sunny.common.Const;
import com.sunny.pojo.Attentions;
import com.sunny.pojo.User;
import com.sunny.service.IAttentionsService;
import com.sunny.utils.PageInformation;
import com.sunny.utils.PageUtil;
import com.sunny.utils.ServerResponse;
import com.sunny.vo.AttentionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @ClassName:
 * @Description:
 * @author: yubao.jin
 * @date: 2018/5/13
 * @version:V1.0
 */
@Controller
@RequestMapping("attention")
public class AttentionsController {
    @Autowired
    IAttentionsService iAttentionsService;

    @RequestMapping("add.do")
    @ResponseBody
    public ServerResponse add(Attentions attentions, HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        attentions.setBuyerid(user.getId());
        return iAttentionsService.add(attentions);
    }

    @RequestMapping("del.do")
    @ResponseBody
    public ServerResponse del(String ids, HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iAttentionsService.del(ids, user.getId());
    }

    @RequestMapping("delAtten.do")
    @ResponseBody
    public ServerResponse delAtten(String ids, HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iAttentionsService.delAttentions(ids, user.getId());
    }

    @RequestMapping("list.do")
    @ResponseBody
    public PageInformation<AttentionVo> list(HttpServletRequest request, Attentions attentions, HttpSession session, Integer type) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(type == 0) {
            attentions.setBuyerid(user.getId());
        } else {
            attentions.setSellerid(user.getId());
        }

        String pageNumber = request.getParameter("offset");
        String pageSize = request.getParameter("limit");
        PageUtil pageUtil = PageUtil.getInstance();
        pageUtil.setPageNum(Integer.parseInt(pageNumber) / Integer.parseInt(pageSize) + 1);
        pageUtil.setPageSize(Integer.parseInt(pageSize));

        return iAttentionsService.list(pageUtil, attentions);
    }

    @RequestMapping("search.do")
    @ResponseBody
    public ServerResponse search(Attentions attentions) {
        return iAttentionsService.search(attentions);
    }
}
